<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class FormInputWidget {

//    var $CI;
    var $debug = true;
    #configuration

    var $field_name ;
    var $field_type;
    var $field_label;
    var $field_attributes ;
    var $default_value = array();
    var $data_source ='';
    #formatters
    var $rowFormatter = "<p>{%label%}{%field%}{%error%}</p>";
    #fields
    var $validators =array() ;

    function forminputwidget($attr='') {
        if(empty($attr)) return ;
        echo ($this->debug)? "\r\n<br>Loading <i>FormInputWidget</i> <b>{$attr['label']}</b>Library..." :'';
        //      $this->CI =& get_instance();
        $this->setAttributes($attr);


    }#end of function

    function setAttributes($attributes) {

        if(isset($attributes) && !empty($attributes)) {

            foreach($attributes as $attrKey=>$attrVal) {
                switch($attrKey) {
                    case 'type':
                        $this->field_type = $attrVal;
                        break;
                    case 'label':
                        $this->field_label = $attrVal;
                        break;
                    case 'default':
                        $this->default_value = $attrVal;
                        break;
                    case 'data_source':
                        $this->data_source = $attrVal;
                        break;
                    case 'extras':
                    //    echo "<b>".$attrKey." > ".$attrVal."</b>";
                        $this->field_attributes = my_build_attributes($attrVal);
                        break;
                }
            }
        }


    }#end of function


    function getType() {

        return $this->field_type;

    }#end of function

    function getName() {
        return $this->field_name;

    }#end of function

    function getLabel() {
        return $this->field_label;

    }#end of function

    function getValidators() {
        return $this->validators;

    }#end of function


    function render($formatter='') {
//        echo "render $this->field_name";
        if(!empty($formatter)) {$this->rowFormatter=$formatter;}
        $html ='';
        $find = array('/\{\%label\%\}/','/\{\%field\%\}/','/\{\%error\%\}/');
        $replace = array($this->field_label,$this->field_type,$this->validators);
        //print_r($this->rowFormatter);
        $html .= preg_replace($find,$replace,$this->rowFormatter);

        return  $html;

        //return ;
    }


    function __toString() {
        return  $this->render();
    }

}#end of class



/* -- INTERFACES -- */

interface iFormInputWidget {
    //function setBaseModel(){/* Model name to bind this field.*/}
    function setLabel();
    function getLabel();
    function setAttributes();
    function getAttributes();
    function setDefault();
    function setValidator();
    function getValidator();
}
